package com.atguigu.gmall.controller;

import com.atguigu.gmall.entity.SkuInfo;
import com.atguigu.gmall.result.RetVal;
import com.atguigu.gmall.service.SkuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/product")
public class SkuController {

    @Autowired
    private SkuInfoService skuInfoService;

    @GetMapping("querySkuInfoByPage/{currentPage}/{limit}")
    public RetVal getPageList(@PathVariable Long currentPage,
                              @PathVariable Long limit) {
        IPage<SkuInfo> skuInfoPage = new Page<>(currentPage, limit);
        IPage page = skuInfoService.selectPage(skuInfoPage);
        return RetVal.ok(page);
    }

    @PostMapping("saveSkuInfo")
    public RetVal saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuInfoService.saveAll(skuInfo);
        return RetVal.ok();
    }

    //上架
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId) throws MyException {
        skuInfoService.onOrOffSaleBySkuId(skuId,1);
        return RetVal.ok();
    }

    //下架
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId) throws MyException {
        skuInfoService.onOrOffSaleBySkuId(skuId, 0);
        return RetVal.ok();
    }

    @GetMapping("getSkuInfoById/{skuId}")
    public SkuInfo getSkuInfoById(@PathVariable Long skuId){
        return skuInfoService.getById(skuId);
    }

    @GetMapping("getSkuPriceNow/{skuId}")
    public BigDecimal getSkuPriceNow(@PathVariable Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return skuInfo.getPrice();
    }

}
